package com.example.zhangxin.zyfypt_124zx.model;


import com.example.zhangxin.zyfypt_124zx.iface.RegisterListener;
import com.example.zhangxin.zyfypt_124zx.iface.Registeriface;
import com.example.zhangxin.zyfypt_124zx.service.RegisterService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * Created by zhangxin on 2017/4/19.
 */
//model,文章模块，实现Articleiface接口
public  class RegisterModel implements Registeriface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造方法
    public RegisterModel(){                                                     //  ScalarsConverterFactory
        retrofit=new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(ScalarsConverterFactory.create()).build();
       /* Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl(BASEURL);
        builder.addConverterFactory(GsonConverterFactory.create());
        retrofit=builder.build();*/
    }
    //获取文章数据
    @Override
    public void getResultList(String username, String password,String tel, final RegisterListener listener) {
        RegisterService registerService
                =retrofit.create(RegisterService.class);
        Call<String> call
                =registerService.getResult(username,password,tel);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful()&&response.body()!=null)
                    listener.onResponse(response.body());
                else
                    listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }
}



